package br.ucb.cesbdao.janelas;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.ParseException;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import org.apache.log4j.Logger;

import br.ucb.cesbdao.enuns.EnumErros;
import br.ucb.cesbdao.enuns.EnumFormatos;
import br.ucb.cesbdao.enuns.EnumLabelAndBotao;
import br.ucb.cesbdao.enuns.EnumResources;
import br.ucb.cesbdao.enuns.EnumTipoTrabalhador;
import br.ucb.cesbdao.enuns.EnumTitulo;
import br.ucb.cesbdao.servicos.JanelaUtil;
import br.ucb.cesbdao.servicos.Servicos;

public class JanelaConexao extends JFrame implements MouseListener, KeyListener, FocusListener {

	private static final long serialVersionUID = 3852760851100136182L;
	static Logger logger = Servicos.carregaLog(JanelaConexao.class);
	private JPanel contentPane;
	private JLabel lblMsgConexo;
	private JFormattedTextField ftCPF;
	private JPasswordField pfSenha;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JRadioButton rdbtnApoio;
	private JRadioButton rdbtnGerencia;
	private JRadioButton rdbtnAfi;
	private String tipoAcesso = "N";
	private JButton btnConectar;


	/**
	 * Create the frame.
	 * @param logger 
	 */
	public JanelaConexao() {
		setVisible(true);
		setIconImage(Toolkit.getDefaultToolkit().getImage(JanelaApoio.class.getResource(EnumResources.ICONE_CESB.getCaminho())));
		setResizable(false);
		setTitle(EnumTitulo.TITULO_JANELA_CONEXAO.getTitulo());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 359, 276);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		addKeyListener(this);
		JPanel panel = new JPanel();

		lblMsgConexo = new JLabel(EnumTitulo.TITULO_JANELA_CONEXAO.getTitulo());
		lblMsgConexo.setBorder(UIManager.getBorder("TitledBorder.border"));
		lblMsgConexo.setFont(new Font(EnumFormatos.FONTE_ARIAL.getFormato(), Font.BOLD, 13));
		lblMsgConexo.setHorizontalTextPosition(SwingConstants.CENTER);
		lblMsgConexo.setHorizontalAlignment(SwingConstants.CENTER);
		lblMsgConexo.setAlignmentX(Component.CENTER_ALIGNMENT);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
				gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
						.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(panel, GroupLayout.PREFERRED_SIZE, 338, Short.MAX_VALUE)
								.addGroup(gl_contentPane.createSequentialGroup()
										.addContainerGap()
										.addComponent(lblMsgConexo, GroupLayout.PREFERRED_SIZE, 326, GroupLayout.PREFERRED_SIZE)))
										.addContainerGap())
				);
		gl_contentPane.setVerticalGroup(
				gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
						.addContainerGap()
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
						.addComponent(lblMsgConexo))
				);

		btnConectar = new JButton(EnumLabelAndBotao.CONECTAR.getTexto());
		btnConectar.addKeyListener(this);
		btnConectar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				conecta();
			}
		});

		ftCPF = new JFormattedTextField();
		ftCPF.addKeyListener(this);
		ftCPF.addMouseListener(this);
		ftCPF.addFocusListener(this);

		try {
			ftCPF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter(EnumFormatos.CPF.getFormato())));
		} catch (ParseException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		ftCPF.setFont(new Font(EnumFormatos.FONTE_ARIAL.getFormato(), Font.BOLD, 20));

		JLabel lblNewLabel = new JLabel(EnumLabelAndBotao.CPF.getTexto());

		JLabel lblSenha = new JLabel(EnumLabelAndBotao.SENHA.getTexto());

		pfSenha = new JPasswordField();
		pfSenha.addKeyListener(this);
		pfSenha.addMouseListener(this);
		pfSenha.addFocusListener(this);
		pfSenha.setEchoChar('●');
		pfSenha.setFont(new Font(EnumFormatos.FONTE_ARIAL.getFormato(), Font.BOLD, 20));

		JLabel label = new JLabel(EnumLabelAndBotao.SEM_TEXTO.getTexto());
		label.setIcon(new ImageIcon(JanelaConexao.class.getResource(EnumResources.ICONE_CESB.getCaminho())));

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(new LineBorder(new Color(192, 192, 192), 1, true), EnumTitulo.TITULO_PERFIL.getTitulo(), TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
						.addGap(9)
						.addComponent(label, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup()
										.addComponent(lblNewLabel)
										.addGap(18)
										.addComponent(ftCPF, GroupLayout.PREFERRED_SIZE, 213, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_panel.createSequentialGroup()
												.addComponent(lblSenha)
												.addPreferredGap(ComponentPlacement.RELATED)
												.addComponent(pfSenha, 213, 213, 213)))
												.addGap(9))
												.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
														.addContainerGap(131, Short.MAX_VALUE)
														.addComponent(btnConectar)
														.addGap(129))
														.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
																.addContainerGap(9, Short.MAX_VALUE)
																.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 323, GroupLayout.PREFERRED_SIZE)
																.addContainerGap())
				);
		gl_panel.setVerticalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup()
										.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
												.addComponent(lblNewLabel)
												.addComponent(ftCPF, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
												.addPreferredGap(ComponentPlacement.UNRELATED)
												.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
														.addComponent(lblSenha)
														.addComponent(pfSenha, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
														.addGroup(gl_panel.createSequentialGroup()
																.addGap(9)
																.addComponent(label)))
																.addPreferredGap(ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
																.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(ComponentPlacement.RELATED)
																.addComponent(btnConectar))
				);

		rdbtnApoio = new JRadioButton(EnumLabelAndBotao.APOIO.getTexto());
		rdbtnApoio.addMouseListener(this);
		rdbtnApoio.addKeyListener(this);
		buttonGroup.add(rdbtnApoio);

		rdbtnAfi = new JRadioButton(EnumLabelAndBotao.AFI.getTexto());
		rdbtnAfi.addMouseListener(this);
		rdbtnAfi.addKeyListener(this);
		buttonGroup.add(rdbtnAfi);

		rdbtnGerencia = new JRadioButton(EnumLabelAndBotao.GERENTE.getTexto());
		rdbtnGerencia.addMouseListener(this);
		rdbtnGerencia.addKeyListener(this);
		buttonGroup.add(rdbtnGerencia);


		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
				gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
						.addContainerGap(61, Short.MAX_VALUE)
						.addComponent(rdbtnApoio)
						.addGap(18)
						.addComponent(rdbtnGerencia)
						.addGap(18)
						.addComponent(rdbtnAfi)
						.addGap(52))
				);
		gl_panel_1.setVerticalGroup(
				gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
								.addComponent(rdbtnApoio)
								.addComponent(rdbtnGerencia)
								.addComponent(rdbtnAfi))
								.addContainerGap(20, Short.MAX_VALUE))
				);
		panel_1.setLayout(gl_panel_1);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}


	@SuppressWarnings("deprecation")
	private void conecta(){
		EnumErros erro = JanelaUtil.getInstance().JanelaConexaoConecta(ftCPF.getText(),pfSenha.getText(),tipoAcesso.charAt(0),this);
		if(erro != null){
			lblMsgConexo.setForeground(erro.getCor());
			lblMsgConexo.setText(erro.getDescricao());
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {

	}

	@Override
	public void mouseEntered(MouseEvent e) {

	}

	@Override
	public void mouseExited(MouseEvent e) {

	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {

		if(e.getSource() == rdbtnGerencia){
			tipoAcesso = EnumTipoTrabalhador.GERENTE.getTipo();
		}
		if(e.getSource() == rdbtnAfi){
			tipoAcesso = EnumTipoTrabalhador.AFI.getTipo();
		}
		if(e.getSource() == rdbtnApoio){
			tipoAcesso = EnumTipoTrabalhador.APOIO.getTipo();
		}
		if(e.getSource() == ftCPF){
			lblMsgConexo.setText(EnumLabelAndBotao.INFORME_CPF.getTexto());
		}
		if(e.getSource() == pfSenha){
			lblMsgConexo.setText(EnumLabelAndBotao.INFORME_SENHA.getTexto());
		}

	}

	@Override
	public void keyPressed(KeyEvent e) {


	}

	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_ENTER){
			conecta();
		}
		if(e.getSource() == rdbtnGerencia){
			tipoAcesso = EnumTipoTrabalhador.GERENTE.getTipo();
		}
		if(e.getSource() == rdbtnAfi){
			tipoAcesso = EnumTipoTrabalhador.AFI.getTipo();
		}
		if(e.getSource() == rdbtnApoio){
			tipoAcesso = EnumTipoTrabalhador.APOIO.getTipo();
		}
		if(e.getSource() == ftCPF){
			lblMsgConexo.setText(EnumLabelAndBotao.INFORME_CPF.getTexto());
		}
		if(e.getSource() == pfSenha){
			lblMsgConexo.setText(EnumLabelAndBotao.INFORME_SENHA.getTexto());
		}

	}

	@Override
	public void keyTyped(KeyEvent arg0) {


	}

	@Override
	public void focusGained(FocusEvent e) {
		if(e.getSource() == ftCPF){
			lblMsgConexo.setText(EnumLabelAndBotao.INFORME_CPF.getTexto());
		}
		if(e.getSource() == pfSenha){
			lblMsgConexo.setText(EnumLabelAndBotao.INFORME_SENHA.getTexto());
		}
	}

	@Override
	public void focusLost(FocusEvent e) {
		if(e.getSource() == ftCPF){
			lblMsgConexo.setText(EnumTitulo.TITULO_JANELA_CONEXAO.getTitulo());
		}
		if(e.getSource() == pfSenha){
			lblMsgConexo.setText(EnumTitulo.TITULO_JANELA_CONEXAO.getTitulo());
		}
	}


}
